1.2: Converting Binary, Decimal, and Hex Numbers 您所在的位置:网站首页 binary number conversion 1.2: Converting Binary, Decimal, and Hex Numbers

1.2: Converting Binary, Decimal, and Hex Numbers

2023-08-08 21:39| 来源: 网络整理| 查看: 265

1.2.4 Converting between binary and hexadecimal

One of the biggest problems with binary is that the numbers rapidly become very hard to read. This is also true in decimal, where there is often a "," inserted between groupings of 103. So for example 1632134 is often written as 1,632,134, which is easier to read.

In binary, something similar is done. Most students are familiar with the term byte, which is 8 bits. But fewer know of a nybble, or 4 bits. 4 bits in binary can represent numbers between 0..15, or 16 values. So values of 4 bits are collected together and create a base 16 number, called a hexadecimal (or simply hex) number. To do this, 16 digits are needed, and arbitrarily the numbers and letters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F were chosen as the 16 digits. The binary numbers corresponding to these 16 digit hex numbers are given in the table below (note, the normal way to indicate a value is in hex is to write a 0x before it So decimal 10 would be 0xA).

Table 1-4: Binary to Hexadecimal Conversion

Binary Number

Hex Digit

Binary Number

Hex Digit

Binary Number

Hex Digit

Binary Number

Hex Digit

0000

0x0

0001

0x1

0010

0x2

0011

0x3

0100

0x4

0101

0x5

0110

0x6

0111

0x7

1000

0x8

1001

0x9

1010

0xA

1011

0xB

1100

0xC

1101

0x D

1110

0xE

1111

0xF

The hex numbers can then be arranged in groups of 4 (or 32 bits) to make it easier to translate from a 32 bit computer.

Note that hex numbers are normally only used to represent groupings of 4 binary digits. Regardless of what the underlying binary values represent, hex will be used just to show what the binary digits are. So in this text all hex values will be unsigned whole numbers.

Most students recognize that a decimal number can be extended by adding a 0 to the left of a decimal number, which does not in any way change that number. For example 0043310 = 043310 = 43310. The same rule applies to binary. So the binary number 1101100012 = 0001101100012.

But why would anyone want to add extra zeros to the left of a number? Because to print out the hex representation of a binary number, I need 4 binary digits to do it. The binary number 1101100012 only has 1 binary digit in the high order byte. So to convert this number to binary it is necessary to pad it with left zeros, which have no effect on the number. Thus 1 10011 00012 = 0001 1011 00012= 0x1B1 in hex. Note that even the hex numbers are often paded with zeros, as the hex number 0x1B1 is normally be written 0x01B1, to get groupings of 4 hex numbers (or 32 bits).

It is often the case where specific bits of a 32 bit number need to be set. This is most easily done using a hex number. For instance, if a number is required where all of the bits except the right left most (or 1) bit of a number is set, you can write the number in binary as:

111111111111111111111111111111102

A second option is to write the decimal value as: 429496729510

Finally the hex value can be written as 0xFFFFFFFE

In almost all cases where specific bits are being set, a hex representation of the number is the easiest to understand and use.



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有